//
//  ZToolControl.m
//  danpin
//
//  Created by chuangjia on 2/11/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "ZToolControl.h"
#import "viewconfig.h"
#import "UILabel+StringFrame.h"
@interface ZToolControl()
{
    
}
@property(nonatomic,strong)UIButton * buyButton;
@property(nonatomic,strong)UIButton * likeButton;
@property(nonatomic,strong)UIButton * commentButton;
@property(nonatomic,strong)UILabel *cmtLable;
@end
@implementation ZToolControl
@synthesize buyButton=_buyButton;
@synthesize buyTitle=_buyTitle;
@synthesize isLove=_isLove;
@synthesize cmtLable=_cmtLable;
@synthesize commentCount=_commentCount;
@synthesize commentButton=_commentButton;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setFrame:frame];
        [self initUIWithType:0];
    }
    return self;
}



-(void)layoutSubviews
{
    [super layoutSubviews];
    
}
-(void)dealloc
{
    
    self.actionBlock=nil;

}

-(void)setCallBack:(void(^)(ZToolControlType,UIView *,id,id))callback
{
    self.actionBlock =callback;
}
-(void)setIsLove:(BOOL)isLove
{
    _isLove=isLove;
    if (isLove) {
        [_likeButton setImage:[UIImage imageNamed:@"like_cheack"] forState:UIControlStateNormal];
    }else{
        [_likeButton setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
    }
}
-(void)setCommentCount:(NSString *)commentCount
{
    if (commentCount==nil || [commentCount length]==0 || [commentCount intValue] < 1) {
        _cmtLable.hidden=YES;
    }else{
        _cmtLable.hidden=NO;

        _cmtLable.text=commentCount;
        CGSize size =[_cmtLable boundingRectWithSize:CGSizeMake(100, 14)];
        if(size.width < 10)
        {
            size.width = 10;
            //最小宽度是10，否则个位数的评论很难看
        }
        _cmtLable.frame =CGRectMake(_commentButton.frame.origin.x+_commentButton.frame.size.width-(size.width+4)/2, (BOTTOM_TOOLBAR_HEIGHT- hightAuto(30))/2, size.width+4, 14);
    }
}
-(void)setBuyTitle:(NSString *)buyTitle
{
    _buyTitle = buyTitle;
    
    if( [_buyTitle isEqualToString:@"去购买"])
    {
        [_buyButton setTitle:@"去购买" forState:UIControlStateNormal];
        [_buyButton setBackgroundColor:UIColorFromRGB(0xDE3B30)];
         _buyButton.tag = 4;
        _buyButton.enabled = YES;
    }
    else if( [_buyTitle isEqualToString:@"一键海淘"])
    {
        [_buyButton setTitle:@"一键海淘" forState:UIControlStateNormal];
        [_buyButton setBackgroundColor:UIColorFromRGB(0xDE3B30)];//只有和价格有关的、购买按钮用红色。
        _buyButton.tag = 5;
        _buyButton.enabled = YES;
    }else if( [_buyTitle isEqualToString:@"已下架"])
    {
        [_buyButton setTitle:@"已下架" forState:UIControlStateNormal];
        [_buyButton setBackgroundColor:UIColorFromRGB(0xf2f2f2)];
        _buyButton.enabled = NO;
    }
    
}

- (void) EnableBuyButton:(BOOL) yesNo
{
    if(yesNo)
    {
        _buyButton.backgroundColor = UIColorFromRGB(0xDE3B30);
        _buyButton.enabled = YES;
    }
    else
    {
        _buyButton.backgroundColor = [UIColor darkGrayColor];
        _buyButton.enabled = NO;
    }
    
}
-(void)initUIWithType:(int)type
{
 //   UIView *bgView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
 //   bgView.backgroundColor=DARK_BACKGROUND_COLOR;
 //   [self addSubview:bgView];
    
    //  背景色黑色半透明
    self.backgroundColor = DARK_BACKGROUND_COLOR;//[UIColor blackColor];
    
   _buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _buyButton.frame = CGRectMake(self.frame.size.width - 100, 0, 100, 40);
    [_buyButton addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [_buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
     _buyButton.titleLabel.font = FONT(15);
    [_buyButton setTitle:@"去购买" forState:UIControlStateNormal];
    [_buyButton setBackgroundColor:UIColorFromRGB(0xDE3B30)];
    _buyButton.tag = 4;
    [self addSubview:_buyButton];
    
    
    _likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _likeButton.frame = CGRectMake(_buyButton.frame.origin.x - PHOTO_FRAME_WIDTH*5, 5, 30, 30);
    _likeButton.tag =3;
    [_likeButton setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
    [_likeButton.imageView setContentMode:UIViewContentModeScaleAspectFill];
    [_likeButton addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [_likeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self addSubview:_likeButton];
    
    _commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _commentButton.frame = CGRectMake(_likeButton.frame.origin.x - PHOTO_FRAME_WIDTH*5, 5, 30, 30);
    _commentButton.tag =2;
    [_commentButton setImage:[UIImage imageNamed:@"discuss"] forState:UIControlStateNormal];
    [_commentButton.imageView setContentMode:UIViewContentModeScaleAspectFill];
    [_commentButton addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [_commentButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self addSubview:_commentButton];
    
    _cmtLable = [[UILabel alloc] initWithFrame:CGRectZero];
    _cmtLable.backgroundColor=UIColorFromRGB(0xCE0202);
    _cmtLable.textColor =[UIColor whiteColor];
    _cmtLable.textAlignment=NSTextAlignmentCenter;
    _cmtLable.layer.cornerRadius=3;
    _cmtLable.layer.masksToBounds=YES;
    _cmtLable.font=FONT(11);
    _cmtLable.hidden=YES;
    [self addSubview:_cmtLable];
    
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.tag =1;
    rightBtn.frame = CGRectMake(PHOTO_TIME_PADDING,  PHOTO_TIME_PADDING, _commentButton.frame.origin.x - PHOTO_FRAME_WIDTH*2, 40-PHOTO_TIME_PADDING*2);
    [rightBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.titleLabel.font = FONT(12);
    rightBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    rightBtn.backgroundColor = [UIColor whiteColor];
    [rightBtn setTitle:@"写评论" forState:UIControlStateNormal];
    rightBtn.layer.cornerRadius = 12;//(值越大，角就越圆)
    rightBtn.layer.masksToBounds = YES;
    rightBtn.layer.borderColor = UIColorFromRGB(0xAAAAAA).CGColor;
    rightBtn.layer.borderWidth = 0.5f;
    
    
    //设置按钮偏移
    rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    rightBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [self addSubview:rightBtn];
    


}
-(void)clickEvent:(id)sender
{
    UIButton*btn =(UIButton*)sender;
    [UIView animateWithDuration:0.5f animations:^{
        btn.transform = CGAffineTransformMakeScale(1.5, 1.5);
    } completion:^(BOOL finished) {
        btn.transform = CGAffineTransformMakeScale(1, 1);
    }];
    
    ZToolControlType type=ZToolControlType_unknow;
    switch (btn.tag) {
        case 1:
        {
            type =ZToolControlType_comment;
        }
            break;
        case 2:
        {
            type =ZToolControlType_commentlist;
        }
            break;
        case 3:
        {
            type =ZToolControlType_like;
        }
            break;
        case 4:
        {
            type =ZToolControlType_buy;
        }
            break;
        case 5:
        {
            type =ZToolControlType_haitao;
        }
            break;
        default:
            break;
    }
    if (self.actionBlock) {
        self.actionBlock(type,self,nil,nil);
    }
}
@end